package com.zhiwei.fast.adapter

import androidx.annotation.LayoutRes
import androidx.appcompat.widget.AppCompatImageView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhiwei.fast.R
import com.zhiwei.fast.data.RandomItem

/**
 * Create by zhiweizhu on 2022/2/26
 */
class LoopAdapter(
    @LayoutRes private val layoutResId: Int,
    data: MutableList<RandomItem>? = null
) : BaseQuickAdapter<RandomItem, BaseViewHolder>(layoutResId, data) {

    override fun getItem(position: Int): RandomItem {
        return data[position % data.size]
    }


    override fun getItemCount(): Int {
        return Int.MAX_VALUE
    }

    override fun getItemViewType(position: Int): Int {
        var count = headerLayoutCount + data.size
        if (count <= 0) {
            count = 1
        }
        return super.getItemViewType(position % count)
    }

    override fun convert(holder: BaseViewHolder, item: RandomItem) {
        val appCompatImageView = holder.getView<AppCompatImageView>(R.id.itemEnvelope)
        Glide.with(context).load(item.src).into(appCompatImageView)
    }
}